<?php 
class WhoisAction extends Action{

	public function index(){
		L('domain', '');
		$this->display();
	}
	
	public function _empty($domain){

		if( !empty($domain) ){
			L('domain', $domain);
			L('content', $this->parseHtml());
		}
		
		$this->display('index');
	}
	
	public function ajax(){
		if( !empty($this->_get['s']) ){
			L('domain', $this->_get['s']);
			L('content', $this->parseHtml());
		}
		$this->ajaxReturn($content);
	}

	
	//设置url目标地址
	private function getUrl(){	
		return 'http://1api.net/whois.php?query='.L('domain')
					.'&btnsearch.x='.round(0,99)
					.'&btnsearch.y='.round(0,99) ;
	}
	
	//curl 获取远程数据
	protected function getContent(){
		$url = $this->getUrl();
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$content = trim(curl_exec($ch));
		curl_close($ch);
		if( empty($content) ){
			$content = L('timeout');
		}
		return $content;
	}
	
	//过滤 html
	private function parseHtml(){
		$content = $this->getContent();
		if( preg_match('/<pre>([\s\S]*)<\/pre>/i', $content, $matches) ){
			$html = $matches[0];
			$html = preg_replace("/<script([^>]*)?>(.*)?<\/script>/si", '', $html);
		}else{
			$html = sprintf( L('notfound'), L('domain') );
		}
		return $html;
	}

}